{extend name="base"/}
{block name="resources"}
<link rel="stylesheet" type="text/css" href="ADMIN_CSS/login.css" />
{/block}
{block name="body"}
<div class="layui-container">
	<div class="layui-form login-form">
		<div class="ns-login-logo">
			<img src="ADMIN_IMG/login/login_logo.png" />
		</div>
		<div class="layui-form-title">
			<h1>多商户后台登录系统</h1>
		</div>

		<div class="layui-form-item">
			<img class="ns-input-icon" src="ADMIN_IMG/login/username.png" />
			<input type="text" name="username" lay-verify="userName" placeholder="请输入用户名" autocomplete="off" class="layui-input">
		</div>
		<div class="layui-form-item">
			<img class="ns-input-icon" src="ADMIN_IMG/login/password.png" />
			<input type="password" name="password" lay-verify="password" placeholder="请输入密码" autocomplete="off" class="layui-input">
		</div>

		{if $admin_login == 1}
		<div class="layui-form-item verify-code-box">
			<input type="text" name="captcha" lay-verify="verificationCode" placeholder="请输入验证码" class="layui-input" value="">
			<div class="verify-code-img">
				<img id='verify_img' src="{$captcha['img']}" alt='captcha' onclick="verificationCode()"/>
			</div>
		</div>
		<input type="hidden" name="captcha_id" value="{$captcha['id']}">
		{/if}

		<div class="layui-form-item ns-login-btn">
			<button class="layui-btn layui-btn-fluid ns-bg-color" lay-submit lay-filter="login">登 录</button>
		</div>
	</div>
	
	<div class="ns-login-bottom">
		{if !empty($copyright.copyright_desc)} {$copyright.copyright_desc} {else /}版权所有 © 2019-2020 山西牛酷信息科技有限公司，并保留所有权利{/if}
	</div>
</div>
{/block}
{block name="script"}
<script>
	layui.use('form', function() {
		var form = layui.form,
			repeat_flag = false; //防重复标识

		/**
		 * 登录
		 */
		form.on('submit(login)', function(data) {

			if (repeat_flag) return false;
			repeat_flag = true;

			$.ajax({
				type: "POST",
				url: "{:url('admin/login/login')}",
				data: data.field,
				dataType: "JSON",
				success: function(res) {
					if (res.code == 0) {
						layer.msg('登录成功',{anim: 5,time: 500},function () {
							window.location = "{:url('admin/index/index')}";
						})
					} else {
						layer.msg(res.message);
						repeat_flag = false;
						verificationCode();
					}
				}
			});
		});

		$(document).keydown(function(event) {
			if (event.keyCode == 13) {
				$(".ns-login-btn button").trigger("click");
			}
		});

		/**
		 * 表单验证
		 */
		form.verify({
			userName: function(value) {
				if (!value) {
					return "用户名不能为空";
				}
			},
			password: function(value) {
				if (!value) {
					return "密码不能为空";
				}
			},
			verificationCode: function(value) {
				if (!value) {
					return "验证码不能为空";
				}
			}

		});
	});
	
	/**
	 * 验证码
	 */
	function verificationCode(){
		$.ajax({
			type: "get",
			url: "{:url('admin/login/captcha')}",
			dataType: "JSON",
			async: false,
			success: function (res) {
				var data = res.data;
				$("#verify_img").attr("src",data.img);
				$("input[name='captcha_id']").val(data.id);
			}
		});
	}
</script>
{/block}